package cn.lili.controller.goods;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
import cn.lili.modules.search.entity.dos.EsSupplierGoodsIndex;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.service.EsGoodsSearchService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ElasticsearchController
 *
 * @author Chopper
 * @version v1.0
 * 2021-03-24 18:32
 */
@RestController
@Api(tags = "ES初始化接口")
@RequestMapping("/goods/elasticsearch")
@RequiredArgsConstructor
@Slf4j
public class ElasticsearchGoodsController {

    private final EsGoodsIndexService esGoodsIndexService;

    private final EsGoodsSearchService goodsSearchService;

    private final GoodsService goodsService;


    @ApiOperation(value = "初始化接口")
    @GetMapping
    public ResultMessage<String> init() {
        log.info("start init");
        esGoodsIndexService.init();
        log.info("end init");
        return ResultUtil.success();
    }

    @ApiOperation(value = "查看es进度")
    @GetMapping("/progress")
    public ResultMessage<Map<String, Long>> getProgress() {
        return ResultUtil.data(esGoodsIndexService.getProgress());
    }

    @ApiOperation(value = "从ES中获取商品信息")
    @GetMapping("/es")
    public ResultMessage<Page<EsGoodsIndex>> getGoodsByPageFromEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
        pageVO.setNotConvert(true);
        Page<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoodsByPage(goodsSearchParams, pageVO, EsGoodsIndex.class,true);
        //如果图片为空，则数据库补全
        esGoodsIndices = goodsService.esGoodsIndicesNull(esGoodsIndices);
        //校验商品是否下架
        esGoodsIndices = goodsService.esGoodsIndicesDown(esGoodsIndices);
        return ResultUtil.data(esGoodsIndices);
    }

    @ApiOperation(value = "搜索会员的商品")
    @GetMapping("/vip/es")
    public ResultMessage<Page<EsGoodsIndex>> vipEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
        return this.getGoodsByPageFromEs(goodsSearchParams, pageVO);
    }

    @ApiOperation(value = "搜索免税商品")
    @GetMapping("/free/es")
    public ResultMessage<Page<EsGoodsIndex>> freeEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
        if (goodsSearchParams.getThirdPartyPlatformType() == null) {
            log.error("商品类型为空: thirdPartyPlatformType");
            throw new ServiceException("商品类型为空");
        }
        goodsSearchParams.setIsFree(true);
        return this.getGoodsByPageFromEs(goodsSearchParams, pageVO);
    }

    @ApiOperation(value = "从ES中获取商品信息")
    @GetMapping("/supplier/es")
    public ResultMessage<Page<EsSupplierGoodsIndex>> getSupplierGoodsByPageFromEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
        pageVO.setNotConvert(true);
        Page<EsSupplierGoodsIndex> esGoodsIndices = goodsSearchService.searchGoodsByPage(goodsSearchParams, pageVO, EsSupplierGoodsIndex.class,false);
        goodsSearchService.searchConversion(esGoodsIndices.getRecords());
        return ResultUtil.data(esGoodsIndices);
    }

    @ApiOperation(value = "从ES中获取相关商品品牌名称，分类名称及属性")
    @GetMapping("/es/related")
    public ResultMessage<EsGoodsRelatedInfo> getGoodsRelatedByPageFromEs(EsGoodsSearchDTO goodsSearchParams, PageVO pageVO) {
        pageVO.setNotConvert(true);
        EsGoodsRelatedInfo selector = goodsSearchService.getSelector(goodsSearchParams, pageVO);
        return ResultUtil.data(selector);
    }

}
